﻿function loadReminder(url) {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        //try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
        //catch (e) {
        xmlhttp = new XMLHttpRequest();
        //}
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    var elem = document.getElementById('divReminderGrid');
    //alert(xmlhttp.responseText);
    //elem.style.display = '';
    //document.getElementById('frmContent').style.display = 'none';

    elem.innerHTML = xmlhttp.responseText;

    //always check reminders regardless of the page the user is viewing!
}

var url = '';
function checkReminders() {
    loadReminder('AjaUtils/ReminderPopup.aspx');
    var grid = document.getElementById("hiddenReminderGrid");
    var gridRows = grid.getElementsByTagName("tr");
    for (var i = 1; i < gridRows.length - 1; i++) {
        //alert(i.toString());
        cells = gridRows[i].getElementsByTagName("td");

        reminderID = cells[0].innerHTML;
        reminderTitle = cells[1].innerHTML;
        reminderDesc = cells[2].innerHTML;

        alarmDateTime = cells[3].innerHTML;

        alarmDateTimeExplode = alarmDateTime.split(' ');
        alarmDate = alarmDateTimeExplode[0];
        alarmTime = alarmDateTimeExplode[1];
        alarmAmPm = alarmDateTimeExplode[2];

        alarmTime = alarmTime + " " + alarmAmPm;

        now = new Date();
        nowMonth = now.getMonth() + 1;
        nowDay = now.getDate();
        nowYear = now.getFullYear();
        nowHour = now.getHours();
        nowMin = now.getMinutes();

        nowDate = nowMonth + "/" + nowDay + "/" + nowYear;

        if (nowHour < 12) {
            AmPm = "AM";
        }
        else {
            AmPm = "PM";
        }

        if (nowHour == 0) {
            nowHour = 12;
        }

        if (nowHour > 12) {
            nowHour = nowHour - 12;
        }

        if (nowMin < 10)
            nowMin = "0" + nowMin;


        nowTime = nowHour + ":" + nowMin + ":00 " + AmPm;
        //alert("current date/time: " + nowDate + " " + nowTime + "\n" + "Alarm date/time: " + alarmDate + " " + alarmTime);
        if (alarmDate == nowDate && alarmTime == nowTime) {
            //alert("Alarm!");
            result = confirm('[REMINDER @ ' + alarmTime + ']\n\n' + 'TITLE:\n' + reminderTitle + '\n\nDESCRIPTION:\n' + reminderDesc +
                                        '\n\n\nPress OK to stop the alarm\nPress CANCEL to postpone the alarm for 5 minutes');
            url = 'AjaUtils/ReminderPopup.aspx';
            if (result == 1) {
                url = url + "?action=stopreminder";
                url = url + "&reminderID=" + URLEncode(reminderID);
                response = PerformReminderAction(url);
                if (response.indexOf("Success") > -1)
                    alert('Alarm stopped');
                else
                    alert('An error occurred while stopping the alarm');
            }
            else {
                alarmTimeExplode = alarmTime.split(':');
                alarmHour = alarmTimeExplode[0];
                alarmMin = alarmTimeExplode[1];

                url = url + "?action=postponereminder";
                url = url + "&reminderID=" + URLEncode(reminderID);
                url = url + "&hour=" + URLEncode(alarmHour);
                url = url + "&min=" + URLEncode(alarmMin);
                url = url + "&AmPm=" + URLEncode(alarmAmPm);

                response = PerformReminderAction(url);
                if (response.indexOf("Success") > -1) {
                    alert('Alarm postponed for 5 minutes');
                    loadXMLDoc('Personal/Reminders.aspx');
                }
                else
                    alert('An error occurred while postponing the alarm');
            }
        }
    }

    setTimeout("checkReminders()", 5000);
}

function PerformReminderAction(url) {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    response = xmlhttp.responseText;
    //alert(response);
    //alert(response.substring(response.indexOf("Line"),response.length));
    response = response.substring(response.indexOf("<response>"), response.indexOf("</response"));
    //alert(response);

    return response;
}        